<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Action\PageJumpAction;
use App\Admin\Extensions\Form\Recommend\Edit;
use App\Http\Traits\GlobalConstant;
use App\Models\Campus;
use App\Models\CourseSection;
use App\Models\Mutual;
use App\Models\Recommend;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;

class RecommendController extends BaseController
{


    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('推荐课程互动列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('推荐课程互动详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑推荐课程互动')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增推荐课程互动')
            ->description('新增推荐课程互动')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Recommend());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', '标题');
                $filter->equal('campusInfo.id', '校区')->select(Campus::where('status', 1)->pluck('name as text', 'id'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('status', '状态')->select(['禁用', '正常']);
            });

        });
//
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', '视频ID')->sortable();
        $grid->column('title', '标题');
        $grid->column('campusInfo.name', '校区')->sortable();
        $grid->column('mutual1', '互动')->expand(function ($model) {
            $list = $model->mutual()->get()->map(function ($item) {
                return $item->only(['question', 'back_answer', 'right_answer', 'score', 'point']);
            });
            return new Table(['问题', '答案列表', '正确答案', '答对积分', '出现点(秒)'], $list->toArray());
        });
        $grid->column('mutual_number', '互动数')->display(function () {
            return Mutual::where('recommend_id', $this->id)->where('status', 1)->count();
        });
        $grid->column('discover', '封面')->lightbox(['width' => 30, 'height' => 30]);
        $grid->column('video_url', '视频')->display(function ($v) {
            return '复制';
        })->copyable();
        $grid->column('hot', '热度')->sortable();
        $grid->column('status', __('状态'))->display(function ($v) {
            return GlobalConstant::_STATUS[$v];
        })->label([0 => 'danger', 1 => 'success'])->sortable();
        $grid->column('created_at', __('created_at'))->sortable();
        $grid->column('updated_at', __('updated_at'));

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->add(new PageJumpAction("编辑", '/admin/recommend-edit?r_id=' . $actions->row->id));
        });

        $grid->disableRowSelector();
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        $grid->disableCreateButton();
        $url  = '/admin/recommend-edit';
        $html = <<<HTML
<div class="btn-group pull-right grid-create-btn" style="margin-right: 10px">
    <a href="$url" class="btn btn-sm btn-success">
        <i class="fa fa-plus"></i>&nbsp;&nbsp;新增推荐视频
    </a>
</div>
HTML;
        $grid->tools->append($html);
        $grid->expandFilter();
        Admin::style('.img-thumbnail {width: 60px;height:60px}');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Course::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Recommend());
        $form->text('title', '标题')->required()->attribute(['autocomplete' => 'off']);
        $form->select('campus_id', '校区')->options(Campus::where('status', 1)->pluck('name as text', 'id'))->required();
        $form->image('discover', '封面')->required()->uniqueName();
        $form->file('video_url', '视频')->uniqueName()->required();
        $form->number('hot', '热度')->default(0);
        $form->text('intro', '视频标签文字')->required();
        $form->radio('status', '状态')->options(GlobalConstant::_STATUS);
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }

    public function recommendEdit(Content $content)
    {
        //tab 配置表单
        $tabMenu = [
            'Recommend_Edit' => Edit::class,
        ];

        $tab         = Tab::forms($tabMenu);
        $recommendId = request('r_id') ?? 0;
        $grid        = new Grid(new Mutual());
        $grid->model()->where(['recommend_id' => $recommendId]);
        $grid->disablePagination();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableColumnSelector();
        $grid->disableCreateButton();
        $url  = '/admin/mutual/create?r_id=' . $recommendId;
        $html = <<<HTML
<div class="btn-group pull-right grid-create-btn" style="margin-right: 10px">
    <a href="$url" class="btn btn-sm btn-success">
        <i class="fa fa-plus"></i>&nbsp;&nbsp;新增互动
    </a>
</div>
HTML;
        $grid->tools->append($html);
        $grid->column('question', '问题')->sortable();
        $grid->column('answer', '答案选项')->sortable()->label();
        $grid->column('right_answer', '正确答案')->sortable();
        $grid->column('point', '出现时间点')->sortable();
        $grid->column('score', '可得积分')->sortable();
        $grid->column('status', __('状态'))->display(function ($v) {
            return GlobalConstant::_STATUS[$v];
        })->label([0 => 'danger', 1 => 'success'])->sortable();
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->add(new PageJumpAction("编辑", '/admin/mutual/' . $actions->row->id . '/edit'));
        });
        $tab->add("互动列表", $grid->render());

//        $mutualGrid = app('App\Admin\Controllers\MutualController')->grid();
//        $tab->add("课程交互", $mutualGrid->render());
        return $content
            ->title('推荐视频')
            ->body($tab);
    }

}
